$(function(){
	systemLog.initTable();
	$('#button1').on('click',systemLog.batchDeleteLog);
	jeDate({
	    dateCell:"#StartTime",
	    format:"YYYY-MM-DD hh:mm:ss",
	    isTime:true, 
	    minDate:"1900-01-01 00:00:00",
	    isClear:true, //是否显示清空
	});
	
	jeDate({
	    dateCell:"#EndTime",
	    format:"YYYY-MM-DD hh:mm:ss",
	    isTime:true, 
	    minDate:"1900-01-01 00:00:00",
	    isClear:true, //是否显示清空
	});
	var statusChangeCall =  function ( $realCheckBox , isSelect ){
		   var tr = $realCheckBox.parent().parent();
//		   if( isSelect ){
//			   //移除所有选中
//			   var tbody = tr.parent();
//			   var allTrs = tbody.children();
//			   allTrs.removeClass("nmsTrSelect");
//			   tbody.find("input[type='checkbox']").prop("checked" , false);
//		   }
		   if( isSelect ){
			   $realCheckBox.prop("checked" , true);
			   tr.addClass("nmsTrSelect");
		   }else{
			   $realCheckBox.prop("checked" , false);
			   tr.removeClass("nmsTrSelect");
		   }
	   }
	   nmsckb.preBindClickEvent(statusChangeCall);
	$('#queryLog').on('click',systemLog.queryLog);
	$('#export').on('click',systemLog.backUpAllLogs);
});

var systemLog = {};

systemLog.CONSTANT = {
        DATA_TABLES : {
            DEFAULT_OPTION : { //DataTables初始化选项
                language: {
                    "sProcessing":   sysi18n.processing_,
                   	"sLengthMenu":   sysi18n.length_menu,
                    "sZeroRecords":  sysi18n.no_matching_result,
					"sInfo":         sysi18n.DT_Info, 
                    "sInfoEmpty":    sysi18n.DT_Info_Empty,
          //          "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                    "sInfoPostFix":  "",
                    "sSearch":       sysi18n.search_,
                    "sUrl":          "",
                    "sEmptyTable":     sysi18n["the data in the table is empty"],
                    "sLoadingRecords": sysi18n.loading_,
                    "sInfoThousands":  ",",
                    "oPaginate": {
                        "sFirst":    sysi18n["first page"],
                        "sPrevious": sysi18n["previous page"],
                        "sNext":     sysi18n["next page"],
                        "sLast":     sysi18n["last page"],
                        "sJump":     sysi18n["jump pages"]
                    },
                    "oAria": {
                        "sSortAscending":  sysi18n.ascending_order,
                        "sSortDescending": sysi18n.descending_order
                    }
                }
            }
        }
	};

systemLog.initTable = function(){
	systemLog.table = $('#LogList').DataTable({
		 "dom": '<"top"l>rt<"bottom"ip><"clear">',
//		"oLanguage":systemLog.CONSTANT.DATA_TABLES.DEFAULT_OPTION.language,
//		"scrollY":false,
//		"scrollX":false,
		language : commonI18n.tableI18n,
		"bAutoWidth": false, 
		"iDisplayLength": 10,
		"bServerSide": true, 
		"ordering": false,
		"sAjaxSource": "tlog/queryLogs",
		"fnServerParams" : function(aoData) {  
            aoData.push({  
                "name" : "username",  
                "value":encodeURI($("#operator").val())  
            },{
            	"name":"StartTime",
            	"value":encodeURI($("#StartTime").val()) 
            },{
            	"name":"EndTime",
            	"value":encodeURI($("#EndTime").val()) 
            },{
            	"name":"SN",
            	"value":encodeURI($("#SN").val()) 
            },{
            	"name":"logType",
            	"value":encodeURI($("#logType").val()) 
            },{
            	"name":"testType",
            	"value":encodeURI($("#testType").val()) 
            }
            
            );  
        },
		columns :[
            {"mData": null, "targets": 0},
            // 复选框
            { className: "td-checkbox",
            	              orderable: false,
            	              width: "30px",
            	              data: null,
            	              render: function (data, type, row, meta) {
            	                 return "<input data-logId='"+data.logId+"' type='checkbox' name='logList' class='nmsckb nmsCheckboxS'  /><label class='nmsckbLabel' ></label>";
            	              }
            	            },
            {"mData": 'devSn'},
            {"mData": null,
              	 render: function (data, type, row, meta) {
              		 if(data.type == 1)
   						return "基测";
              		 if(data.type == 2)
    					return "复测";
              		 return "";
                   }
              },
            {"mData": 'timeStamp'},
            {"mData": 'costTime'},
            {"mData": 'descr'},
//            {"mData":'groupID'},
            {"mData":'res'},
            {"mData":'testtype'},
            {"mData":'user'},
            {"mData": null,
           	 render: function (data, type, row, meta) {
						return "<h4 style='display: inline'><a data-logId='"+data.logId+"' onclick='systemLog.deleteLog(this)'><i class='glyphicon glyphicon-remove'></i></a></h4>";
				
                }
           }
        ],
        
        "fnDrawCallback" :function(){
        	var api = this.api();
        	//给行加序号
        	api.column(0).nodes().each(function(cell, i) {
        		cell.innerHTML =  api.context[0]._iDisplayStart + i +1;
        	}); 
        	
        },
      
        
	})
	//checkbox全选
    $("#checkAll").on("click", function () {
        if ($(this).prop("checked") == true) {
            $("input[name='logList']").prop("checked", $(this).prop("checked"));
            $("input[name='logList']").parents("tr").addClass("nmsTrSelect");
        } else {
            $("input[name='logList']").prop("checked", false);
            $("input[name='logList']").parents("tr").removeClass("nmsTrSelect");
        }
    });

}

systemLog.queryLog = function(){
	systemLog.table.ajax.reload();
}
systemLog.deleteLog = function(e){
	sysShowConfirm(function(){
		var logId=[];
		logId.push($(e).attr("data-logId"));
		var url = 'tlog/deleteLogByID';
		var data = { "logId[]" : logId};
		var successCallBack = function(data) {
			if( data.result ){
		           sysShowSuccessTip( sysi18n.Delete_success );
//		           systemLog.table.ajax.reload();
		           systemLog.table.draw(false);
			}else{
				sysShowTip( sysi18n.Delete_failed );
			}
	    }
		
		$.sysAjax( url , data , successCallBack );
	},function(){
		layer.closeAll();
		return false;
	},sysi18n.remove_confirm);
	
}

systemLog.batchDeleteLog = function(){
	var logId=[];
	$("#LogList tbody tr").children().find("input").each(function() {
		if ($(this).prop("checked") == true) {
			logId.push($(this).attr("data-logId"));
	    }
	});
	if(logId.length==0){
		sysShowTip(sysi18n["Please select an option at least"]);
		return false;
	}
	sysShowConfirm(function(){
		var url = 'tlog/deleteLogByID';
		var data = { "logId[]" : logId};
		var successCallBack = function(data) {
			if( data.result ){
		           sysShowSuccessTip( sysi18n.Delete_success );
//		           systemLog.table.ajax.reload();
		           systemLog.table.draw(false);
			}else{
				sysShowTip( sysi18n.Delete_failed );
			}
	    }
		
		$.sysAjax( url , data , successCallBack );
	},function(){
		layer.closeAll();
		return false;
	},sysi18n.remove_confirm);
	
}

systemLog.backUpAllLogs = function(){
	var url = "tlog/backUpAllAlert";
	
	var StartTime = $("#StartTime").val();
	var EndTime = $("#EndTime").val();
	var SN = $("#SN").val();
	var username = $("#operator").val();
	var testType = $("#testType").val();
	var logType = $("#logType").val();
	
	$("#toDownload").attr( "src" , url+"?StartTime="+StartTime+"&EndTime="+EndTime
			+"&SN="+SN
			+"&username="+username
			+"&testType="+testType
			+"&logType="+logType
			+"&pageSize=-1"
			+"&pageNo=-1"
	);
}

